﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// 构建管理器
    /// 用于分策略游戏打包
    /// </summary>
    public class BuildManager
    {
        /// <summary>
        /// 构建游戏
        /// </summary>
        /// <param name="buildStrategyy">构建策略</param>
        public static void BuildGame(IBuildStrategy buildStrategyy)
        {
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
            buildPlayerOptions.scenes = GetBuildScene(buildStrategyy);
            buildPlayerOptions.target = buildStrategyy.GetBuildTarget();
            buildPlayerOptions.locationPathName = buildStrategyy.GetLocationPathName();
            buildStrategyy.BeforeBuildProject();
            BuildPipeline.BuildPlayer(buildPlayerOptions);
        }

        /// <summary>
        /// 从构建策略中获取构建场景
        /// 场景为空则获取BuildSetting场景列表
        /// </summary>
        /// <param name="buildStrategy"></param>
        /// <returns></returns>
        private static string[] GetBuildScene(IBuildStrategy buildStrategy)
        {
            string[] sceneArray = buildStrategy.GetBuildSceneList();
            if (sceneArray != null
                && sceneArray.Length > 0)
                return sceneArray;

            EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
            sceneArray = new string[scenes.Length];
            for (int i = 0; i < scenes.Length; i++)
            {
                sceneArray[i] = scenes[i].path;
            }
            return sceneArray;
        }
    }
}